package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.linlinjava.litemall.db.dao.*;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
public class LitemallBrandInfoService {
    @Resource
    LitemallBrandInfoMapper brandInfoMapper;
    @Resource
    LitemallBrandAppendMapper brandAppendMapper;
    @Resource
    LitemallBrandQualificationMapper brandQualificationMapper;
    @Resource
    LitemallBrandCategoryMapper brandCategoryMapper;
    @Resource
    LitemallCategoryMapper categoryMapper;
    @Resource
    LitemallBrandCommentMapper brandCommentMapper;
    @Resource
    LitemallCommodityService commodityService;

    /**
     * @Description  获取公司信息
     * @Author Riko
     * @Date 2019/9/24
     **/
    public LitemallBrandInfo getInfoByBrandId(Integer brandId) {
        LitemallBrandInfoExample example = new LitemallBrandInfoExample();
        example.or().andBrandIdEqualTo(brandId);
        return brandInfoMapper.selectOneByExampleWithBLOBs(example);
    }

    /**
     * @Description  获取公司员工
     * @Author Riko
     * @Date 2019/9/24
     **/
    public List<LitemallBrandAppend> getStaffByBrandId(Integer brandId) {
        LitemallBrandAppendExample example = new LitemallBrandAppendExample();
        example.or().andBrandIdEqualTo(brandId).andAppendTypeEqualTo("staff");
        return brandAppendMapper.selectByExample(example);
    }

    /**
     * @Description  获取公司资质banner
     * @Author Riko
     * @Date 2019/9/24
     **/
    public List<LitemallBrandQualification> getQualificationByBrandId(Integer brandId) {
        LitemallBrandQualificationExample example = new LitemallBrandQualificationExample();
        example.or().andBrandIdEqualTo(brandId).andIsHasEqualTo(1);
        return brandQualificationMapper.selectByExampleWithBLOBs(example);
    }
    
    /**
     * @Description  获取公司服务项目
     * @Author Riko
     * @Date 2019/9/24
     **/
    public List<LitemallBrandCategory> getCategoryByBrandId(Integer brandId) {
        LitemallBrandCategoryExample example = new LitemallBrandCategoryExample();
        example.or().andBrandIdEqualTo(brandId);
        List<LitemallBrandCategory> list = brandCategoryMapper.selectByExample(example);
        for (LitemallBrandCategory brandCategory : list) {
            brandCategory.setCategory(categoryMapper.selectByPrimaryKey(brandCategory.getCategoryId()));
        }
        return list;
    }
    
    /**
     * @Description  获取公司首页链接
     * @Author Riko
     * @Date 2019/9/24
     **/
    public String getHomePageByBrandId(Integer brandId) {
        LitemallBrandInfoExample example = new LitemallBrandInfoExample();
        example.or().andBrandIdEqualTo(brandId);
        return brandInfoMapper.selectOneByExample(example).getHomepage();
    }

    public void add(LitemallBrandInfo brandInfo) {
        brandInfoMapper.insertSelective(brandInfo);
    }

    public void deleteById(Integer id) {
        LitemallBrandInfoExample example = new LitemallBrandInfoExample();
        example.or().andBrandIdEqualTo(id);
        brandInfoMapper.deleteByExample(example);
    }

    public void update(LitemallBrandInfo brandInfo) {
        brandInfoMapper.updateByPrimaryKeySelective(brandInfo);
    }

    public List<LitemallBrandInfo> findByBrandId(Integer id) {
        LitemallBrandInfoExample example = new LitemallBrandInfoExample();
        example.or().andBrandIdEqualTo(id);
        return brandInfoMapper.selectByExampleWithBLOBs(example);
    }

    public void updateByBrandId(LitemallBrandInfo brandInfo) {
        brandInfoMapper.updateByPrimaryKeySelective(brandInfo);
    }

    /**
     * @Description  新增公司评论
     * @Author Riko
     * @Date 2019/9/25
     **/
    public void insertNewComment(Integer brandId, String name, String mail, String comment ,String remark) {
        LitemallBrandComment brandComment = new LitemallBrandComment();
        brandComment.setBrandId(brandId);
        brandComment.setSignature(name);
        brandComment.setMail(mail);
        brandComment.setComment(comment);
        brandComment.setRemark(remark);
        brandCommentMapper.insertSelective(brandComment);
    }

    /**
     * @Description  读取公司相关的评论
     * @Author Riko
     * @Date 2019/9/25
     **/
    public List<LitemallBrandComment> qurreyComment(Integer brandId, Integer page, Integer pageSize) {
        LitemallBrandCommentExample example = new LitemallBrandCommentExample();
        example.or().andBrandIdEqualTo(brandId);
        PageHelper.startPage(page, pageSize);
        return brandCommentMapper.selectByExample(example);
    }

    /**
     * @Description  读取公司评论的总条数
     * @Author Riko
     * @Date 2019/10/9
     **/
    public int getCommentNum(Integer brandId) {
        LitemallBrandCommentExample example = new LitemallBrandCommentExample();
        example.or().andBrandIdEqualTo(brandId);
        return (int)brandCommentMapper.countByExample(example);
    }

    /**
     * @Description  通过二级域名获取公司ID
     * @Author Riko
     * @Date 2019/9/25
     **/
    public Integer getBrandIdByUrl(String serverName) {
        //获取二级域名
        String secDomainName = serverName.substring(0, serverName.indexOf("."));
        if(serverName.equals("www")){
            //如果
        }
        if (secDomainName.contains("://")) {
            secDomainName = secDomainName.substring(secDomainName.indexOf("://") + 3);
        }
        LitemallBrandInfoExample example = new LitemallBrandInfoExample();
        example.or().andHomepageEqualTo(secDomainName);
        example.or().andHomepageLike(secDomainName + ".%");
        LitemallBrandInfo info = brandInfoMapper.selectOneByExample(example);
        if (info == null) {
            return null;
        }
        else {
            return info.getBrandId();
        }
    }


    public PageInfo<LitemallCommodity> appendBrandInfo(PageInfo<LitemallCommodity> pageInfo) {
        List<LitemallCommodity> commodityList = pageInfo.getList();
        for (LitemallCommodity commodity : commodityList) {
            commodity.setBrandInfo(getInfoByBrandId(commodity.getBrandId()));
        }
        pageInfo.setList(commodityList);
        return pageInfo;
    }

    public LitemallCommodity appendBrandInfo(LitemallCommodity commodity) {
        LitemallBrandInfo info = getInfoByBrandId(commodity.getBrandId());
        List<Integer> list = new LinkedList<>();
        list.add(info.getBrandId());
        info.setCommodityNum(commodityService.countLitemallCommodity(null, null, list));
        commodity.setBrandInfo(info);
        return commodity;
    }

    public LitemallCommodityWithBLOBs appendBrandInfo(LitemallCommodityWithBLOBs commodity) {
        LitemallBrandInfo info = getInfoByBrandId(commodity.getBrandId());
        List<Integer> list = new LinkedList<>();
        list.add(info.getBrandId());
        info.setCommodityNum(commodityService.countLitemallCommodity(null, null, list));
        commodity.setBrandInfo(info);
        return commodity;
    }




}
